package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Policy;
import cn.wolfcode.crm.mapper.PolicyMapper;
import cn.wolfcode.crm.mapper.ProductMapper;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.PolicyQueryObject;
import cn.wolfcode.crm.service.IPolicyService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PolicyServiceImpl implements IPolicyService {

    @Autowired
    private PolicyMapper policyMapper;
    @Override
    public void insert(Policy policy, Long[] pIds) {

        //获取登录用户
        Employee inputUesr = (Employee) SecurityUtils.getSubject().getPrincipal();
        policy.setInputUser(inputUesr);
        policy.setApplyDate(new Date());
        String dateSn = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        policy.setSn("62170020" + dateSn + "4328");
        policy.setState(Integer.valueOf(0));
        policyMapper.insert(policy);

        if (pIds != null) {

            for (Long pId : pIds) {
                //保存保单和保险种类的关系
                policyMapper.savePolicyProductRelation(policy.getId(),pId);
            }
        }



    }

    @Override
    public void updateByPrimaryKey(Policy policy, Long[] pIds) {

        policyMapper.updateByPrimaryKey(policy);

        policyMapper.deletePolicyProductRelation(policy.getId());
        if (pIds != null) {

            for (Long pId : pIds) {
                //保存保单和保险种类的关系
                policyMapper.savePolicyProductRelation(policy.getId(),pId);
            }
        }

    }

    @Override
    public PageResult query(PolicyQueryObject qo) {
        int totalCount = policyMapper.queryForCount(qo);
        if(totalCount == 0){
            return new PageResult();
        }
        List<Policy> list = policyMapper.queryForList(qo);
        return new PageResult(totalCount,list);
    }

    @Override
    public Policy selectByPrimaryKey(Long policyId) {
        Policy policy = policyMapper.selectByPrimaryKey(policyId);

        return policy;
    }

    @Override
    public void changeState(Long id) {

        policyMapper.changeState(id,Integer.valueOf(1));
    }

    @Override
    public void refuse(Long id, String remark) {

        policyMapper.refuse(id,remark,Integer.valueOf(3));
    }

    @Override
    public void audit(Long id) {

        Policy policy = policyMapper.selectByPrimaryKey(id);
        policy.setCheckDate(new Date());
        policy.setState(Integer.valueOf(2));
        Employee auditor = (Employee) SecurityUtils.getSubject().getPrincipal();
        policy.setAuditor(auditor);
        policyMapper.audit(policy);
    }

    @Override
    public void roll(Long id, String remark) {

        Policy policy = policyMapper.selectByPrimaryKey(id);
        policy.setCheckDate(new Date());
        policy.setState(Integer.valueOf(3));
        policy.setRemark(remark);
        Employee auditor = (Employee) SecurityUtils.getSubject().getPrincipal();
        policy.setAuditor(auditor);
        policyMapper.roll(policy);
    }

    @Override
    public List<Policy> getAllSnInfo() {
        return policyMapper.getAllSnInfo();
    }

    @Override
    public PageResult queryForLoss() {
        List<Policy> rows = policyMapper.getAllSnInfo();
        return new PageResult(rows.size(),rows);
    }
}
